package com.hrt.datasender.datacollect.drive.ttmb;

import java.util.HashMap;

import com.btcode.common.MyLog;
import com.btcode.log.ILog;
import com.hrt.datasender.datacollect.drive.DriveConfigModel;
import com.hrt.datasender.datacollect.drive.DriveTimer;
import com.hrt.datasender.datadao.model.BcDevicesModel;
import com.hrt.datasender.mq.MqKit;
import com.hrt.datasender.mq.model.SerialModel;

/**
 * modbus数据询问器
 * 
 * @author gislin
 *
 */
public class TTMBSerialDataAsker extends DriveTimer {
    // 客户端发送01 03 00 00 F1 D8
    private final byte[] askMsg = {(byte) 0x01,(byte) 0x03,(byte) 0x00,(byte) 0x00,(byte) 0xF1,(byte) 0xD8};

    private ILog log = null;

    private SerialModel askModel = null;

    @Override
    public void executeTimer(DriveConfigModel configs, BcDevicesModel deviceData) {

        try {
            log = MyLog.getInstance().getLogger(getClass().getName() + "." + deviceData.getDeviceName());
            askData(deviceData, configs);
            log.info("询问");
        } 
        catch (Exception e) {
            log.error("执行定时任务出错", e);
        }

    }

    private void askData(BcDevicesModel device, DriveConfigModel deviceConfigs) {
        SerialModel askModel = getAskDataModel(device, deviceConfigs);
        MqKit.productAskDataMsgToSerialMQ(askModel);
    }

    /**
     * 获取串口询问报文对象
     * 
     * @param device
     * @param deviceConfigs
     * @return
     */
    private SerialModel getAskDataModel(BcDevicesModel device, DriveConfigModel deviceConfigs) {
        HashMap<String, String> configs = deviceConfigs.getConfig();
        String slaveNo = configs.get("slaveNo");
        if (askModel == null) {
            askModel = new SerialModel(askMsg, device.getComNum(), slaveNo);
        }

        return askModel;
    }
}
